package com.example.lots_of_demo.controller;

import com.example.lots_of_demo.model.REnum;
import com.example.lots_of_demo.model.Res;
import com.example.lots_of_demo.model.Role;
import com.example.lots_of_demo.po.PermissionGroupPo;
import com.example.lots_of_demo.po.PermissionGroupRoleIdPo;
import com.example.lots_of_demo.service.PermissionService;
import com.example.lots_of_demo.utils.R;
import com.example.lots_of_demo.vo.PermissionGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : LZJ
 * @description : code for LZJ
 * @date : 2021/2/16
 */
@RestController
@RequestMapping("/permission")
public class PermissionController extends BaseController{

    @Autowired
    private PermissionService permissionService;

    @GetMapping("/getPermission")
    @PreAuthorize("hasAuthority('RES_getPermission')")
    public R getPermission(){
        List<PermissionGroupVo> permissions = permissionService.getPermissionGroup();
        return R.createSuccess(REnum.RSuccessResponse.SELECT_SUCCESS_RESPONSE, permissions);
    }

    @GetMapping("/getPermissionByRole")
    @PreAuthorize("hasAuthority('RES_getPermissionByRole')")
    public R getPermissionByRole(Role role){
        List<Integer> defaultCheckedKeys = permissionService.getCheckedPermissionGroupByRoleId(role.getId());
        return R.createSuccess(REnum.RSuccessResponse.SELECT_SUCCESS_RESPONSE, defaultCheckedKeys);
    }

    @PostMapping("/endowPermission")
    @PreAuthorize("hasAuthority('RES_endowPermission')")
    public R endowPermission(@RequestBody PermissionGroupRoleIdPo permissionGroupRoleIdPo){
        return permissionService.endowPermission(permissionGroupRoleIdPo);
    }
}
